package cn.ad.server.web.spring.service.actions.handler;

import cn.ad.server.common.utils.BeanUtils;
import cn.ad.server.web.spring.model.sts.STSAdResponse;
import cn.ad.server.domain.ad.Creative;
import cn.ad.server.domain.ad.InteractionApp;
import cn.ad.server.domain.ad.InteractionRedirects;
import cn.ad.server.domain.enumration.ActionType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author dongbin.yu
 * @from 2016-06-25
 * @since V1.0
 */
public abstract class AbstractAdFormatHandler implements AdFormatHandler {

	protected static Logger logger = LoggerFactory.getLogger(OriginAdFormatHandler.class);

	protected void interactionHandler(Creative matchCreative, STSAdResponse.Creative creative, int os) {

		// 填充event
		creative.setAdmark(
				BeanUtils.convertIosUrl("http://alicdn.lieying.cn/sentshow/imgs/tmp/tsa_ad_logo106x36.png", os));
		STSAdResponse.Event event = new STSAdResponse.Event();
		int interactionType = matchCreative.getInteractionType();
		if (interactionType == ActionType.LINK_JUMP.getValue()) {
			InteractionRedirects interactionRedirects = matchCreative.getInteractionRedirects();
			event.setEventKey(ActionType.LINK_JUMP.getValue());
			event.setEventValue(interactionRedirects.getTargetUrl());
		} else if (interactionType == ActionType.APP_DOWNLOAD.getValue()) {
			InteractionApp interactionApp = matchCreative.getInteractionApp();
			event.setEventKey(ActionType.APP_DOWNLOAD.getValue());
			event.setEventValue(interactionApp.getDownloadUrl());

			STSAdResponse.App app = new STSAdResponse.App();
			app.setAppName(interactionApp.getName());
			int appType = interactionApp.getAppType();
			if (appType == 1) {
				app.setAppPackage(interactionApp.getApkName());
			} else if (appType == 2) {
				app.setAppPackage(interactionApp.getStoreId());
			}

			app.setAppSize(interactionApp.getAppVolume());
			if (interactionApp.getAppClass() != null) {
				app.setAppCategory(interactionApp.getAppClass().getName());
			}
			creative.setApp(app);
		}

		creative.setEvent(new STSAdResponse.Event[] { event });

	}

}
